package com.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.wx.model.NewsModel;
import com.wx.service.INews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/back/news")
public class NewsController {
    @Autowired
    INews iNews;

    @RequestMapping("/conditionFind")
    public JSONObject conditionFind(@RequestBody NewsModel newsModel){
//        PageModel pageModel =newsModel.getPageModel();
//        pageModel.setPageNum(pageModel.getPageNum()==null || "".equals(pageModel.getPageNum())? 1 :(pageModel.getPageNum()));
//        pageModel.setPageSize(pageModel.getPageSize()==null || "".equals(pageModel.getPageSize()) ? 10:(pageModel.getPageSize()));
        List<NewsModel> list=iNews.conditionFind(newsModel);
        int number=iNews.getCont(newsModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("number",number);
        return jsonObject;
    }

    @RequestMapping("/newsAdd")
    public JSONObject newsAdd(@RequestBody NewsModel newsModel){
        int count=iNews.newsAdd(newsModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("count",count);
        return jsonObject;
    }

    @RequestMapping("/newsFindById")
    public JSONObject newsFindById(@RequestBody NewsModel newsModel){
        NewsModel newsModel1=iNews.newsFindById(newsModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("newsModel",newsModel1);
        return jsonObject;
    }

    @RequestMapping("/newsUpdate")
    public JSONObject newsUpdate(@RequestBody NewsModel newsModel){
        int conut=iNews.newsUpdate(newsModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("conut",conut);
        return jsonObject;
    }

    @RequestMapping("/newsDeleteById")
    public JSONObject newsDeleteById(@RequestBody NewsModel newsModel){
       int count= iNews.newsDeleteById(newsModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("count",count);
        return jsonObject;
    }
}
